package cn.service.web;

import cn.service.entiy.Goods;
import cn.service.entiy.Order;
import cn.service.entiy.User;
import cn.service.service.FeignService;
import cn.service.service.GoodsFeignService;
import cn.service.service.OrderFeignService;
import cn.service.service.UserFeignService;
import com.lmk.core.web.support.api.RequestResult;
import com.lmk.core.web.support.api.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@Api
@RestController
@RequestMapping("api/client")
public class FeignController {

    @Autowired
    private UserFeignService userFeignService;

    @Autowired
    private GoodsFeignService goodsFeignService;

    @Autowired
    private OrderFeignService orderFeignService;

    /**
     * 注册
     * @author 二饭
     * @email 1786705138@qq.com
     */
    @ApiOperation("用户注册")
    @GetMapping("reg")
    public RequestResult reg(User user){
        RequestResult result = new RequestResult();
        Boolean is=userFeignService.regUser(user);
        if(is){
            result.setCode(new StatusCode(200,"注册成功"));
        }else {
            result.setCode(new StatusCode(204,"注册失败"));
        }

        return result;
    }

    /**
     * 登录
     * @author 二饭
     * @email 1786705138@qq.com
     */
    @ApiOperation("用户登录")
    @GetMapping("login")
    public RequestResult login(User user){
        RequestResult result = new RequestResult();
        Boolean is=userFeignService.login(user);
        if(is){
            result.setCode(new StatusCode(200,"登录成功"));
        }else {
            result.setCode(new StatusCode(204,"登录失败"));
        }

        return result;
    }

    /**
     * 添加商品
     * @author 二饭
     * @email 1786705138@qq.com
     */
    @ApiOperation("添加商品")
    @GetMapping("addGoods")
    public RequestResult addGoods(Goods goods){
        RequestResult result = new RequestResult();
        Boolean is=goodsFeignService.addGoods(goods);
        if(is){
            result.setCode(new StatusCode(200,"添加商品成功"));
        }else {
            result.setCode(new StatusCode(204,"添加商品失败"));
        }

        return result;
    }

    /**
     * 购买商品
     * @author 二饭
     * @email 1786705138@qq.com
     */
    @ApiOperation("购买商品")
    @GetMapping("buy")
    public RequestResult buy(Order order){
        if(order.getUserId()==null || order.getGoodsId()==null || order.getQuality()==null || order.getSummary()==null){
            return new RequestResult(new StatusCode(204,"用户ID、商品ID、数量不能为空"));
        }
        RequestResult result = new RequestResult();

        int id=orderFeignService.buyGoods(order);
        if(id>=1){
            result.setCode(new StatusCode(200,"购买成功"));
//            result.appendData("订单ID",id);
        }else {
            result.setCode(new StatusCode(204,"购买失败"));
        }

        return result;
    }

    /**
     * 获取账单流水
     * @author 二饭
     * @email 1786705138@qq.com
     */
    @ApiOperation("根据日期获取流水")
    @GetMapping("getBill")
    public RequestResult getBill(String begin,String end){
        RequestResult result=new RequestResult();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        Long beginDate;
        Long endDate;
        try {
            beginDate=format.parse(begin).getTime();
            endDate= format.parse(end).getTime();
            ArrayList<Order> orderList=orderFeignService.getBill(beginDate,endDate);
            result.appendData("账单",orderList);
        } catch (ParseException e) {
            result.setCode(new StatusCode(204,"时间格式应该为2020-08-11"));
            e.printStackTrace();
        }

        return result;
    }


}
